<?php


require_once(dirname(__FILE__)."/defines.php");


date_default_timezone_set('Asia/Chongqing');


function data_to_str($content)
{
    if (is_object($content))
    {
        return json_encode($content).' - '.get_class($content);
    }

    if (empty($content))
    {
        if (is_array($content))
        {
            return '[]';
        }

        if (is_integer($content))
        {
            return 0;
        }

        if (is_string($content))
        {
            return 'null';
        }

        if (is_bool($content))
        {
            return 'false';
        }
    }

    if (is_array($content))
    {
        return json_encode($content);
    }

    if (is_string($content) || is_integer($content) || is_bool($content))
    {
        return $content;
    }

    return $content.' - '.gettype($content);
}

function write_log($hint, $data, $dir = '')
{
    $path  = __DIR__.'/../logs/'.$dir;
    if (!is_dir($path))
    {
        mkdir($path);
    }

    $file   = $path.$hint.'_'.date("Ymd").".log";
    $fp     = fopen($file, "a");

    $log    = data_to_str($data);

    try
    {
        $stack  = debug_backtrace();

        if ($fp)
        {
            $item  = $stack[1];

            if (isset($item['file']))
            {
                $file   = basename($item['file']);
                $line   = $item['line'];

                fwrite($fp, "[".date("H:i:s")."][$file:$line] - $log\n");
            }
            else
            {
                $clss  = $item['class'];
                $func  = $item['function'];

                fwrite($fp, "[".date("H:i:s")."][$clss] - $func\n");
            }            
        }
    }
    catch (Exception $e)
    {
        fwrite($fp, "!!!!!!! ERROR !!!!!!!\n");
        fwrite($fp, data_to_str($e->getMessage())."\n!!!!!!! ERROR !!!!!!!\n");
    }

    fclose($fp);

    return 0;   
}

function logd($content, $dir = '')
{
    if (LOG_LEVEL > LOG_DEBUG)
    {
        return;     
    }

    write_log('debug', data_to_str($content), $dir);
}

function logi($content, $dir = '')
{
    if (LOG_LEVEL > LOG_INFO)
    {
        return;     
    }
    
    write_log('info', data_to_str($content), $dir);
}

function logw($content, $dir = '')
{
    if (LOG_LEVEL > LOG_WARN)
    {
        return;     
    }
    
    write_log('warn', data_to_str($content), $dir);
}

function loge($content, $dir = '')
{
    if (LOG_LEVEL > LOG_ERROR)
    {
        return;     
    }
    
    write_log('error', data_to_str($content), $dir);
}

function logf($content, $dir = '')
{
    if (LOG_LEVEL > LOG_FATAL)
    {
        return;     
    }
    
    write_log('fatal', data_to_str($content), $dir);
}

function logt($data)
{
    $path  = __DIR__.'/../logs/';
    if (!is_dir($path))
    {
        mkdir($path);
    }

    $file   = $path.date("Ymd").".log";
    $fp     = fopen($file, "a");

    $log    = data_to_str($data);

    try
    {
        $stack  = debug_backtrace();

        if ($fp)
        {
            $item   = $stack[0];
            $file   = basename($item['file']);
            $line   = $item['line'];
            
            fwrite($fp, "---------------- start ----------------\n[".date("H:i:s")."][0][$file:$line] - $log\n");

            for ($i = 1; $i < count($stack); $i ++)
            {
                $item  = $stack[$i];
                $func  = $item['function'];

                if (isset($item['file']))
                {
                    $file   = basename($item['file']);
                    $line   = $item['line'];

                    fwrite($fp, "[".date("H:i:s")."][$i][$file:$line] - $func\n");
                }
                else
                {
                    $clss  = $item['class'];
    
                    fwrite($fp, "[".date("H:i:s")."][$i][$clss] - $func\n");
                }
            }

            fwrite($fp, "---------------- end ----------------\n");
            
        }
    }
    catch (Exception $e)
    {
        fwrite($fp, "!!!!!!! ERROR !!!!!!!\n");
        fwrite($fp, data_to_str($e->getMessage())."\n!!!!!!! ERROR !!!!!!!\n");
    }

    fclose($fp);

    return 0;   
}

